<?php

class PersonaController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
     	# $this->_helper->layout->disableLayout ();
		//$this->_helper->viewRenderer->setNoRender ();
		 echo "indexActionPersona";
	  
    }
	
	public function personaAction(){
		echo $this->view->util()->registerScriptJSController($this->getRequest());
	}
	public function busquedaperAction(){
		
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		//if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			//$bq = $this->_request->getPost('term');
			$bq = strtolower($_GET["term"]);
			//if (!$bq) return;
			$cn = new Model_Data();
			$parameter[]=array("@pcod_pers",'');
			$parameter[]=array("@pdesc_per",$bq);
			$parameter[]=array("@pest_pers",'1');
			$result_bus = $cn ->ejec_store_procedura_sql("MPersona_list", $parameter);
			
			$result = array();
			foreach ($result_bus as $value) {
				array_push($result, array("id"=>$value[0], "label"=>$value[1], "value" => strip_tags($value[1])));
				//array_push($result, array("id"=>$value[0], "label"=>$value[1], "value" => $value[1]));
				if (count($result) > 5)
					break;
			}
			echo json_encode($result);
			
		//}
		
	}
	public function buscarAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('tip');
			$val = $upper->filter($this->_request->getPost('val'));
			$chk = $this->_request->getPost('chk');
			$cn = new Model_Data();
			$parameter[]=array("@pcod_pers",($tip=='0' ? $val:''));
			$parameter[]=array("@pdesc_per",($tip=='1' ? $val:''));
			$parameter[]=array("@pest_pers",$chk);
			$result_bus = $cn ->ejec_store_procedura_sql("MPersona_list", $parameter);

			if($result_bus==null || count($result_bus)==0){
				echo "Sin Resultados";
			}else{
				$tb="";
				foreach ($result_bus as $value) {
					$tb .= '<tr id="'.$value[0].'">';
					$tb .='<td>'.$value[0].'</td>';
					$tb .='<td>'.$value[1].'</td>';
					$tb .='<td>'.$value[3].'</td>';
					$tb .='<td>'.$value[4].'</td>';
					$tb .= '</tr>';
				}
				//echo '<table border="1"><tr><td>';
				echo '<table id="tbl_result" class="ui-widget ui-widget-content tablesorter">'.
						'<thead>'.
							'<tr class="ui-widget-header">'.
								'<tr class="ui-widget-header">'.
									'<th width="100px">Codido</th>'.
									'<th width="279px">Nombre y Apellidos</th>'.
									'<th width="120px">Tipo Doc</th>'.
									'<th width="120px">Num Doc</th>'.
								'</tr>'.
							'</tr>'.
						'</thead>'.
						'<tbody>'.
							$tb.
						'</tbody>'.
					'</table>';

				//echo "</td></tr><tr><td>";
				echo $this->view->TableSorter()->ScriptDivPage();
				//echo $this->view->util()->registerScriptJSController($this->getRequest());
				//echo "</td></tr></table>";
				//$(\"#tbl_result\").tablesorter({sortList:[[0,0]], widgets: ['zebra'],headers: { 5:{sorter: false}}}).tablesorterPager({container: $(\"#pager\")});
				echo "<script type=\"text/javascript\">
						$(function() {		
							$(\"#tbl_result\").tablesorter({sortList:[[0,0]]})
							$(\"#tbl_result\").tablesorterPager({
								container: $(\"#pager\"),
								afterPagingCallBack: function(){
									AbrirMenu();
								}
								});
							
						});	
					</script>";
				
				
			}
			
		}
	}
	
	public function mantenimientoAction(){
		
     	echo $this->view->util()->registerScriptJSController($this->getRequest());
		$this->_helper->layout->disableLayout ();
		#$this->_helper->viewRenderer->setNoRender ();
		$type = $this->_request->getParam('type', '');
		$cod = $this->_request->getParam('codItem', '');
		$cn = new Model_Data();
		$pintar = New Libreria_Pintar();
		$parameter[]=array("@pcod_pers",$cod);
		$parameter[]=array("@pdesc_per",'');
		$parameter[]=array("@pest_pers",'');

		$TDparameter[]=array("@pcod_tipdociden","");
		$TDparameter[]=array("@pdesc_tipo",'');
		$TDparameter[]=array("@pest_tipdociden",'1');

		$TDresult_bus = $cn ->ejec_store_procedura_sql("MTipoDociden_list", $TDparameter);
		for ($x=0;$x<count($TDresult_bus);$x++)
    		$cboTD[] = array($TDresult_bus[$x][0],$TDresult_bus[$x][1]);
		
		$TPparameter[]=array("@pcod_tippers","");
		$TPparameter[]=array("@pdesc_tippers",'');
		$TPparameter[]=array("@pest_tippers",'1');

		$TPresult_bus = $cn ->ejec_store_procedura_sql("MTipoPers_list", $TPparameter);
		foreach ($TPresult_bus as $value) 
    		$cboTP[] = array($value[0],$value[1]);
		
		if ($type=='N'){
			$val[] = array('#cbocod_tipodoc',$pintar->ContenidoCombo($cboTD,'null'),'html');
			$val[] = array('#cbocod_tipopers',$pintar->ContenidoCombo($cboTP,'null'),'html');
		}elseif($type=='M' || $type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("MPersona_list", $parameter);
			$val[] = array('#hdcodigo',$result_bus[0][0],'val');
			$val[] = array('#txtds_apepat',$result_bus[0][5],'val');
			$val[] = array('#txtnum_apemat',$result_bus[0][6],'val');
			$val[] = array('#txtds_nom',$result_bus[0][7],'val');
			$val[] = array('#cbocod_tipodoc',$pintar->ContenidoCombo($cboTD,$result_bus[0][2]),'html');
			$val[] = array('#txtnum_doc',$result_bus[0][4],'val');
			$val[] = array('#txtds_direc',$result_bus[0][8],'val');
			$val[] = array('#txtnu_telf',$result_bus[0][9],'val');
			$val[] = array('#cbocod_tipopers',$pintar->ContenidoCombo($cboTP,$result_bus[0][10]),'html');
		}/*elseif($type=='E'){
			$result_bus = $cn ->ejec_store_procedura_sql("MPersona_list", $parameter);
			$val[] = array('#hdcodigo',$result_bus[0][0],'val');
			$val[] = array('#txtds_apepat',$result_bus[0][5],'val');
			$val[] = array('#txtnum_apemat',$result_bus[0][6],'val');
			$val[] = array('#txtds_nom',$result_bus[0][7],'val');
			$val[] = array('#cbocod_tipodoc',$pintar->ContenidoCombo($cboTD,$result_bus[0][2]),'html');
			$val[] = array('#txtnum_doc',$result_bus[0][4],'val');
			$val[] = array('#txtds_direc',$result_bus[0][8],'val');
			$val[] = array('#txtnu_telf',$result_bus[0][9],'val');
			
		}*/
		
		$pintar->PintarValor($val);
	}

	public function executemanteAction(){
		$this->_helper->getHelper ( 'ajaxContext' )->initContext ();
		
		if ($this->getRequest ()->isXmlHttpRequest ()) {
			$this->_helper->viewRenderer->setNoRender ();
			$this->_helper->layout->disableLayout ();
			
			$upper = new Zend_Filter_StringToUpper();
			$upper->setEncoding('UTF-8');
			$tip = $this->_request->getPost('type');
			$hdcodigo = $upper->filter($this->_request->getPost('hdcodigo'));
			$cbocod_tipopers = $upper->filter($this->_request->getPost('cbocod_tipopers'));
			$txtds_apepat = $upper->filter($this->_request->getPost('txtds_apepat'));
			
			$txtnum_apemat = $upper->filter($this->_request->getPost('txtnum_apemat'));
			$txtds_nom = $upper->filter($this->_request->getPost('txtds_nom'));
			$cbocod_tipodoc = $upper->filter($this->_request->getPost('cbocod_tipodoc'));
			$txtnum_doc = $upper->filter($this->_request->getPost('txtnum_doc'));
			$txtds_direc = $upper->filter($this->_request->getPost('txtds_direc'));
			$txtnu_telf = $upper->filter($this->_request->getPost('txtnu_telf'));
			$st_estado = $upper->filter($this->_request->getPost('st_stado'));
			
			$cn = new Model_Data();

			if($tip=='M' ||$tip=='N' ){
				$parameter[]=array("@ptype",$tip);
				$parameter[]=array("@pcod_pers",$hdcodigo);
				$parameter[]=array("@pdesc_apater",$txtds_apepat);
				$parameter[]=array("@pdesc_amater",$txtnum_apemat);
				$parameter[]=array("@pdesc_nombre" ,$txtds_nom);
				$parameter[]=array("@pcod_tipdociden" ,$cbocod_tipodoc);
				$parameter[]=array("@pnum_doc" ,$txtnum_doc);
				$parameter[]=array("@pdesc_direccion" ,$txtds_direc);
				$parameter[]=array("@pnum_telf" ,$txtnu_telf);
				$parameter[]=array("@pcod_tippers" ,$cbocod_tipopers);
				$parameter[]=array("@pest_pers" ,$st_estado);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MPersona_InsertUpdate", $parameter);
				 	
			}elseif($tip=='D'){
	
				$parameter[]=array("@pcod_pers",$hdcodigo);
				$parameter[]=array("@puser_reg",$this->view->util()->getUser());
				$parameter[]=array("@phost_reg",$this->view->util()->getHost());
				$result = $cn ->ejec_store_procedura_sql("MPersona_Delete", $parameter);
			}
			if($result[0][0]=='1') echo "Transaccion Exitosa";
			if($result[0][0]=='2') 
			 echo  $this->view->util()->getTextError($result[0]);
		}
		
	}

}

